<template>
  <div class="home-container">
    <!-- 搜索栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <template #title>
        <van-button to="/Search" class="search-btn" type="primary" size="small" round icon="search">搜索</van-button>
      </template>
    </van-nav-bar>

    <!-- 频道+文章区域 -->
    <van-tabs animated swipeable swipe-threshold="3" v-model:active="active">
      <!-- 导航 -->
      <van-tab v-for="channel in dataList.TabList" :title="channel.name" :key="channel.id">
        <!-- 文章列表内容 -->
        <atricle-list :channel="channel"></atricle-list>
      </van-tab>
      <!-- 汉堡包三杠 点击跳出频道栏编辑-->
      <template #nav-right>
        <div class="placeholder"></div>
        <div class="hamburger-btn" @click="isPopupsShow = true">
          <van-icon center class="iconfont headline-gengduo"></van-icon>
        </div>
      </template>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup v-model:show="isPopupsShow" closeable position="bottom" :style="{ height: '100%' }">
      <popup-content @UpdateActive="onUpdateActive" :active="active" :myChannels="dataList.TabList">
      </popup-content>
    </van-popup>
  </div>
</template>

<script>
import AtricleList from './components/articleList'
import PopupContent from './components/popupContent.vue'
import { getUserTabAPI } from '@/api/user'
import { ref, onMounted, reactive, computed, toRaw } from 'vue'
import { useStore } from 'vuex'
import { Toast } from 'vant'
import { getItem } from '@/utils/storage'
export default {
  name: 'Home',
  components: {
    AtricleList,
    PopupContent
  },
  setup () {
    const active = ref(0)
    const isPopupsShow = ref(false) // 控制弹出层显示状态
    const dataList = reactive({
      TabList: []
    })
    const store = useStore()
    const user = computed(() => toRaw(store.state.user))
    const getHomeTab = async () => {
      try {
        if (user.value) {
          // 登录获取用户列表
          const { data } = await getUserTabAPI()
          // console.log(data)
          dataList.TabList = data.data.channels
        } else {
          // 未登录
          const localChannels = await getItem('TOUDIAO_NAMELIST')
          if (localChannels) {
            // 本地存储有数据就用
            dataList.TabList = localChannels
          } else {
            // 本地存储没有数据 就请求获取默认列表
            // 不强制用户登录，匿名用户返回后台设置的默认频道列表
            const { data } = await getUserTabAPI()
            dataList.TabList = data.data.channels
          }
        }
      } catch (err) {
        Toast.fail('获取数据失败')
      }
    }
    // 子组件popupContent触发，非编辑中是跳转到对应Home的页面
    const onUpdateActive = (index, isPopups) => {
      if (isPopups) {
        active.value--
      } else {
        active.value = index
      }
      isPopupsShow.value = isPopups
    }
    onMounted(() => {
      getHomeTab()
    })

    return {
      active,
      dataList,
      isPopupsShow,
      onUpdateActive
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 174px;

  .van-tab__panel-wrapper {
    min-height: 84vh;
  }

  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
  }

  .placeholder {
    flex-shrink: 0;
    width: 56px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 56px;
    height: 82px;
    background-color: #fff;
    background-color: rgba(255, 255, 255, 0.902);

    i.iconfont {
      font-size: 33px;
    }

    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }

}
</style>
